/*  Copyright (c) 2010 Xiaoyun Zhu
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy  
 *  of this software and associated documentation files (the "Software"), to deal  
 *  in the Software without restriction, including without limitation the rights  
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 *  copies of the Software, and to permit persons to whom the Software is  
 *  furnished to do so, subject to the following conditions:
 *  
 *  The above copyright notice and this permission notice shall be included in  
 *  all copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
 *  THE SOFTWARE.  
 */
package cn.kk.exia;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class ProxyPanel extends javax.swing.JPanel {
  private static final long serialVersionUID = 4558328191741920665L;
  private int               action           = -1;
  private final JDialog     parent;

  /**
   * Creates new form ProxyPanel
   * 
   * @param dialog
   */
  public ProxyPanel(final JDialog dialog) {
    this.parent = dialog;
    this.initComponents();
    final String host = System.getProperty("http.proxyHost");
    final String port = System.getProperty("http.proxyPort");
    final String user = System.getProperty("http.proxyUser");
    final String pass = System.getProperty("http.proxyPassword");
    if (Helper.isNotEmptyOrNull(host)) {
      this.tfHost.setText(host);
    }
    if (Helper.isNotEmptyOrNull(port)) {
      this.tfPort.setText(port);
    }
    if (Helper.isNotEmptyOrNull(user)) {
      this.tfUser.setText(user);
    }
    if (Helper.isNotEmptyOrNull(pass)) {
      this.tfPass.setText(pass);
    }
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">
  private void initComponents() {

    this.tfHost = new javax.swing.JTextField();
    this.lblHost = new javax.swing.JLabel();
    this.lblPort = new javax.swing.JLabel();
    this.lblUser = new javax.swing.JLabel();
    this.lblPass = new javax.swing.JLabel();
    this.tfPort = new javax.swing.JTextField();
    this.tfUser = new javax.swing.JTextField();
    this.tfPass = new javax.swing.JTextField();
    this.btnOK = new javax.swing.JButton();
    this.btnClear = new javax.swing.JButton();

    this.lblHost.setText("服务器");

    this.lblPort.setText("端口");

    this.lblUser.setText("用户名");

    this.lblPass.setText("密码");

    this.btnOK.setText("保存设置");
    this.btnOK.addActionListener(new java.awt.event.ActionListener() {
      @Override
      public void actionPerformed(final java.awt.event.ActionEvent evt) {
        ProxyPanel.this.btnOKActionPerformed();
      }
    });

    this.btnClear.setText("取消代理");
    this.btnClear.addActionListener(new java.awt.event.ActionListener() {
      @Override
      public void actionPerformed(final java.awt.event.ActionEvent evt) {
        ProxyPanel.this.btnClearActionPerformed();
      }
    });

    final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
        layout
            .createSequentialGroup()
            .addContainerGap()
            .addGroup(
                layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(
                        layout.createSequentialGroup().addComponent(this.lblHost).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(this.tfHost))
                    .addGroup(
                        layout
                            .createSequentialGroup()
                            .addGroup(
                                layout
                                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(
                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addComponent(this.lblPort)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                    .addGroup(
                                        layout
                                            .createSequentialGroup()
                                            .addGroup(
                                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(this.lblPass)
                                                    .addComponent(this.lblUser)).addGap(10, 10, 10)))
                            .addGroup(
                                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(this.tfPass).addComponent(this.tfPort)
                                    .addComponent(this.tfUser, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)))
                    .addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(this.btnClear)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(this.btnOK))).addContainerGap()));
    layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
        layout
            .createSequentialGroup()
            .addContainerGap()
            .addGroup(
                layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(this.lblHost)
                    .addComponent(this.tfHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(
                layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(this.lblPort)
                    .addComponent(this.tfPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(
                layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(this.lblUser)
                    .addComponent(this.tfUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(
                layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(this.lblPass)
                    .addComponent(this.tfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(this.btnOK).addComponent(this.btnClear))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
  }// </editor-fold>

  void btnOKActionPerformed() {
    this.action = JOptionPane.OK_OPTION;
    this.parent.setVisible(false);
  }

  void btnClearActionPerformed() {
    this.tfHost.setText("");
    this.tfPort.setText("");
    this.tfUser.setText("");
    this.tfPass.setText("");
  }

  public int getAction() {
    return this.action;
  }

  // Variables declaration - do not modify
  private javax.swing.JButton    btnClear;

  private javax.swing.JButton    btnOK;

  private javax.swing.JLabel     lblHost;

  private javax.swing.JLabel     lblPass;

  private javax.swing.JLabel     lblPort;

  private javax.swing.JLabel     lblUser;

  private javax.swing.JTextField tfHost;

  private javax.swing.JTextField tfPass;

  private javax.swing.JTextField tfPort;

  private javax.swing.JTextField tfUser;

  // End of variables declaration

  public String getHost() {
    return this.tfHost.getText();
  }

  public String getPort() {
    return this.tfPort.getText();
  }

  public String getUser() {
    return this.tfUser.getText();
  }

  public String getPass() {
    return this.tfPass.getText();
  }
}
